package com.woniu.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
//import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handler -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns("/api/users/login")
                .excludePathPatterns("/api/users/getUserInfo")
                .excludePathPatterns("/api/users/register")
                .excludePathPatterns("/api/users/getEmailCode")
                .excludePathPatterns("/api/users/checkCode")
                .excludePathPatterns("/api/category/faceCategory")
                .excludePathPatterns("/api/course/getRecommendCourses")
                .excludePathPatterns("/api/course/getPopularCourses")
                .excludePathPatterns("/oss/upload")
                .excludePathPatterns("/api/ai/chatStream")
                .excludePathPatterns("/oss/upload")
                .excludePathPatterns("/oss/VideoCoverText");
    }

}


//webflux版本
//package com.woniu.config;
//
//import cn.dev33.satoken.exception.NotLoginException;
//import cn.dev33.satoken.reactor.filter.SaReactorFilter;
//import cn.dev33.satoken.router.SaRouter;
//import cn.dev33.satoken.stp.StpUtil;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebFilter;
//import org.springframework.web.server.WebFilterChain;
//import reactor.core.publisher.Mono;
//
//import java.util.Arrays;
//import java.util.List;
//
//@Configuration
//public class SaTokenConfigure {
//
//    @Bean
//    public WebFilter getSaReactorFilter() {
//        return (exchange, chain) -> {
//            // 获取当前请求路径
//            String path = exchange.getRequest().getPath().value();
//
//            // 定义需要放行的接口路径
//            List<String> excludedPaths = Arrays.asList(
//                    "/api/users/login",
//                    "/api/users/getUserInfo",
//                    "/api/users/register",
//                    "/api/users/getEmailCode",
//                    "/api/users/checkCode",
//                    "/oss/upload"
//            );
//
//            // 判断请求路径是否在放行列表中
//            boolean isExcluded = excludedPaths.stream()
//                    .anyMatch(p -> path.startsWith(p));
//
//            if (isExcluded) {
//                // 放行请求，不进行拦截
//                return chain.filter(exchange);
//            } else {
//                // 执行Sa-Token过滤器逻辑
//                return new SaReactorFilter()
//                        .addInclude("/**")
//                        .setAuth(obj -> StpUtil.checkLogin())  // 验证登录
//                        .filter(exchange, chain);
//            }
//        };
//    }
//}
